#ifndef _WORKERMANAGER_HXX_
#define _WORKERMANAGER_HXX_

#include <mytypes.h>
#include <mymutex.hxx>
#include <mythread.hxx>

#include <set>
#include <list>

class FileLog;
class AttackWorker;
class BridgeConn;
typedef struct tagBridgeMsg tBridgeMsg;

using namespace std;

class WorkerManager : public Thread
{
public:
 // 
  WorkerManager(BridgeConn* bridgeconn, FileLog *filelog=0);
  ~WorkerManager();

  void Shutdown(void);

protected:
  //
  virtual void Execute(Thread::Arg);


private:
  WorkerManager(const WorkerManager &model);

  void HandleMsg(const tBridgeMsg* bmsg);

  void RequestCjhRoot(void);
  void Request(tUInt32 tom);

  void Attack(int port, tUInt32 atktype, tUInt32 atknum);
  void StopAttack(void);
  
  // Clear the obsolete/dead workers.
  void ClearWorkers(void);
  
  int GetCjh(string& cjh, string& clhx) const;
  string GetProxy(void) const;

  void SaveProxies(const std::vector<string>& proxies);

  static const int WorkerMax = 360;

  BridgeConn* _bridgeconn;

  list<AttackWorker*> _workers;
  Mutex _workers_mutex;

  string _cjhroot;
  string _clxh;

  std::set<string> _proxies;
  mutable Mutex _proxies_mutex;
  mutable std::set<string>::const_iterator _proxies_iter;

  bool _is_shutdown_requested;
  FileLog* _filelog;
};

#endif // _WORKERMANAGER_HXX_
